<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2016/12/6
 * Time: 16:39
 */

namespace SDXApi\Model;


use Think\Model;

/**
 * Class GoodsWidModel
 * @package SDXApi\Model
 */
class GoodsWidModel extends Model
{
	/**
	 * 默认仓库1的表
	 * @var string
	 */
	protected $tableName = 'goods_wid_1';

	/**
	 * 设置仓库id表名
	 * Created by Jerry
	 * @param $wid
	 * @return $this
	 */
	public function setTableNameByWid($wid)
	{
//		$this->tableName = 'goods_wid_' . $wid;
		$this->table('sdx_goods_wid_' . $wid);
		return $this;
	}

	/**
	 * 获取商品列表
	 * Created by Jerry
	 * @return mixed
	 */
	public function get_shelves_service_goods_for_list()
	{
		$field = 'sdx_goods_wid_8.goods_id, sdx_goods_wid_8.goods_sn, sdx_goods_wid_8.is_on_sale, sdx_goods_wid_8.goods_sn';

		return $this
			->join("sdx_shelves_goods AS sg ON sg.goods_sn = sdx_goods_wid_8.goods_sn")
			->select();
	}


	/**
	 * Created by Jerry
	 * @param $goods_id
	 * @param $wid
	 * @return mixed
	 */
	public function get_is_presell($goods_id, $wid)
	{
		$where = array(
			'goods_id'	=>	$goods_id,
		);

		$field = 'goods_name, goods_thumb, goods_img, goods_brief, is_presell, promote_price, promote_start_date, promote_end_date, goods_number, warn_number, original_img';

		return $this->table('sdx_goods_wid_'. $wid)->field($field)->where($where)->find();
	}

	/**
	 * 返回指定仓库指定id商品详情
	 * Created by Jerry
	 * @param $goods_id
	 * @return mixed
	 */
	public function get_goods_info_by_goods_id($goods_id)
	{
		$where = array(
			'goods_id'		=>	$goods_id,
			'is_delete'		=>	0,
			'is_on_sale'	=>	array('NEQ', 0),
			'original_img'	=>	array('NEQ', ''),
		);

		$field = 'goods_id, goods_name, goods_thumb, goods_img, goods_brief, is_presell, promote_price, promote_start_date, promote_end_date, goods_number, warn_number, original_img, is_on_sale, promote_goods_number, promote_today_number, is_buynow, spec_unit, spec_value, min_buy_num, delivery_type, delivery_cycle';

		return $this->field($field)->where($where)->find();
	}


	/**
	 * 返回秒杀商品详情
	 * Created by Jerry
	 * @param $goods_id
	 * @return mixed
	 */
	public function findBuyNowGoodsInfoByGoodsId($goods_id)
	{
		$where = array(
			'goods_id'		=>	$goods_id,
			'is_buynow'		=>	1,
			'is_delete'		=>	0,
			'is_on_sale'	=>	array('NEQ', 0),
			'original_img'	=>	array('NEQ', ''),
		);

		$field = 'goods_id, goods_name, goods_thumb, goods_img, goods_brief, is_presell, promote_price, promote_start_date, promote_end_date, goods_number, warn_number, original_img, is_on_sale, promote_goods_number, promote_today_number, is_buynow, delivery_type, delivery_cycle, warn_number, spec_unit, spec_value, min_buy_num, promote_number';

		return $this->field($field)->where($where)->find();
	}
}